package icu.qx.pro.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import icu.qx.pro.domain.ProWorkOrder;
import icu.qx.pro.domain.ProWorkTask;
import icu.qx.pro.service.IProWorkOrderService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import icu.qx.pro.mapper.ProWorkReportMapper;
import icu.qx.pro.domain.ProWorkReport;
import icu.qx.pro.service.IProWorkReportService;

/**
 * 过程检验Service业务层处理
 *
 * @author yong
 * @date 2024-06-30
 */
@Service
public class ProWorkReportServiceImpl implements IProWorkReportService {
    @Autowired
    private ProWorkReportMapper proWorkReportMapper;
    @Autowired
    private IProWorkOrderService proWorkOrderService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 查询过程检验
     *
     * @param reportCode 过程检验主键
     * @return 过程检验
     */
    @Override
    public ProWorkReport selectProWorkReportByReportCode(String reportCode) {
        return proWorkReportMapper.selectProWorkReportByReportCode(reportCode);
    }

    /**
     * 查询过程检验列表
     *
     * @param proWorkReport 过程检验
     * @return 过程检验
     */
    @Override
    public List<ProWorkReport> selectProWorkReportList(ProWorkReport proWorkReport) {
        return proWorkReportMapper.selectProWorkReportList(proWorkReport);
    }

    /**
     * 新增过程检验
     *
     * @param proWorkReport 过程检验
     * @return 结果
     */
    @Override
    public int insertProWorkReport(ProWorkReport proWorkReport) {
        proWorkReport.setCreateTime(DateUtils.getNowDate());
        proWorkReport.setCreateBy(SecurityUtils.getUsername());
        // 获取工单信息
        ProWorkOrder proWorkOrder = proWorkOrderService.selectProWorkOrderByWorkOrderCode(proWorkReport.getReportOCode());
        proWorkReport.setReportOCodeName(proWorkOrder.getWorkOrderName());
        proWorkReport.setReportOBatch(proWorkOrder.getWorkOrderBatch());
        proWorkReport.setReportPCode(proWorkOrder.getWorkOrderProduct());
        return proWorkReportMapper.insertProWorkReport(proWorkReport);
    }

    /**
     * 修改过程检验
     */
    @Override
    public int updateProWorkReport(ProWorkReport proWorkReport) {
        proWorkReport.setUpdateTime(DateUtils.getNowDate());
        proWorkReport.setUpdateBy(SecurityUtils.getUsername());
        return proWorkReportMapper.updateProWorkReport(proWorkReport);
    }

    @Override
    public int calcSal(ProWorkReport proWorkReport) {
        ProWorkTask proWorkTask = new ProWorkTask();
        proWorkTask.setWtaskCode(proWorkReport.getReportTCode());
        proWorkTask.setWtaskRNum(proWorkReport.getReportCompleteNum());
        proWorkTask.setWtaskREnd(DateUtils.getNowDate());
        proWorkTask.setWtaskStatus("6");
        rabbitTemplate.convertAndSend("proStatusConsumer.exchange", "completeWorkTask", proWorkTask);
        return updateProWorkReport(proWorkReport);
    }

    /**
     * 批量删除过程检验
     *
     * @param reportCodes 需要删除的过程检验主键
     * @return 结果
     */
    @Override
    public int deleteProWorkReportByReportCodes(String[] reportCodes) {
        return proWorkReportMapper.deleteProWorkReportByReportCodes(reportCodes);
    }
}
